package com.qf.listener;

import org.apache.ibatis.io.Resources;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.IOException;
import java.util.Properties;
/**
 * projectName: 2103--RBAC
 *
 * @author: MaTianWei
 * time: 2021/7/9 22:20
 * description:加载外部的匿名配置文件和认证的配置
 */
@WebListener
public class PropertiesLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //程序启动触发
        Properties anonymity = new Properties();
        Properties authentication = new Properties();
        //读取
        try {
            anonymity.load(Resources.getResourceAsStream("rbac/anonymity.properties"));
            authentication.load(Resources.getResourceAsStream("rbac/authentication.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //共享域
        ServletContext application = sce.getServletContext();
        application.setAttribute("anonymity",anonymity);
        application.setAttribute("authentication", authentication);
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //程序结束触发
        ServletContext application = sce.getServletContext();
        application.removeAttribute("anonymity");
        application.removeAttribute("authentication");
    }
}